package controller;

import model.EditBean;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import model.AreaName;
import model.CreateBean;
import model.RoleName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
import javax.validation.Valid;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ZYT on 2015/6/23.
 */
@Controller("e")
@RequestMapping("/")
public class Edit {
    @Autowired
    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }


    @RequestMapping("edit.do")
    public ModelAndView _edit(HttpServletRequest request, @ModelAttribute("edit") EditBean editBean) {
        ModelAndView modelAndView = new ModelAndView("edit");
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            PreparedStatement sql = connection.prepareStatement("SELECT a.username,a.name,a.password,a.t_area_id,a.t_role_id from t_user AS a WHERE username= ?");
            String name = request.getParameter("username");
            sql.setString(1, name);
            ResultSet resultSet = sql.executeQuery();
            while (resultSet.next()) {
                editBean.setUsername(resultSet.getString(1));
                editBean.setName(resultSet.getString(2));
                editBean.setPassword(resultSet.getString(3));
                editBean.setArea_id(resultSet.getInt(4));
                editBean.setRole_id(resultSet.getInt(5));
            }
            Map[] maps = map();
            modelAndView.addObject("rolemap", maps[0]);
            modelAndView.addObject("areamap", maps[1]);
            modelAndView.addObject("user", editBean);
            return modelAndView;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return modelAndView;
    }

    @RequestMapping("edit.action.do")
    public ModelAndView _edit_action(HttpServletRequest request, @Valid @ModelAttribute("edit") EditBean editBean, BindingResult bindingResult) {
        ModelAndView success = new ModelAndView("redirect:/index3.do");
        ModelAndView failed = new ModelAndView("edit");
        Map[] maps = map();
        failed.addObject("rolemap", maps[0]);
        failed.addObject("areamap", maps[1]);
        failed.addObject("user",editBean);
        if (bindingResult.hasErrors()) {
            return failed;
        } else {
            Connection connection = null;
            try {
                connection = dataSource.getConnection();
                PreparedStatement sql = connection.prepareStatement("update t_user set name=?,password=?,t_area_id=?,t_role_id=? where username=?;");
                sql.setString(1, editBean.getName());
                sql.setString(2, editBean.getPassword());
                sql.setInt(3, editBean.getArea_id());
                sql.setInt(4, editBean.getRole_id());
                sql.setString(5, editBean.getUsername());
                int n = sql.executeUpdate();
                if (n > 0) {
                    return success;
                }

            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return failed;
    }


    public Map[] map() {
        Map[] maps = new Map[2];
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            PreparedStatement sql = connection.prepareStatement("SELECT * from t_role");
            ResultSet resultSet = sql.executeQuery();

            Map map = new HashMap();
            while (resultSet.next()) {
                map.put(resultSet.getInt("id"), resultSet.getString("name"));
            }
            maps[0] = map;

            PreparedStatement sql1 = connection.prepareStatement("SELECT * from t_area");
            ResultSet resultSet1 = sql1.executeQuery();
            Map map1 = new HashMap();
            while (resultSet1.next()) {
                map1.put(resultSet1.getInt("id"), resultSet1.getString("name"));
            }

            maps[1] = map1;

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return maps;
    }
}
